package com.elitesland.system.entity;

import com.elitesland.core.base.BaseModel;
import com.elitesland.system.annotation.Comment;
import com.elitesland.system.convert.BooleanToIntegerConverter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Objects;

/**
 * <pre>
 * [功能说明]
 * </pre>
 *
 * @author Mir
 * @date 2020/6/15
 */
@Entity
@Table(name = "sys_permission")
@org.hibernate.annotations.Table(appliesTo = "sys_permission", comment = "系统权限表，包括URL、操作")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "系统权限表", description = "系统权限表，包括URL、操作")
@DynamicUpdate
public class SysPermissionDO extends BaseModel implements Serializable {
    private static final long serialVersionUID = 6810626977765149177L;

    @ApiModelProperty(value = "权限代码")
    @Comment("权限代码")
    @Column(length = 64, nullable = false, unique = true)
    @NotBlank(message = "权限代码不能为空")
    private String code;

    @ApiModelProperty(value = "权限名称")
    @Comment("权限名称")
    @Column(length = 16, nullable = false)
    @NotBlank(message = "权限名称不能为空")
    private String name;

    @ApiModelProperty(value = "权限类型")
    @Comment("权限类型")
    @Column()
    private Integer permType;

    @ApiModelProperty(value = "父权限ID")
    @Comment("父权限ID")
    @Column()
    private Long parentId;

    @ApiModelProperty(value = "权限层级路径")
    @Comment("权限层级路径")
    @Column()
    private String path;

    @ApiModelProperty(value = "权限层级")
    @Comment("权限层级")
    @Column()
    private Integer permLevel;

    @ApiModelProperty("排列顺序")
    @Comment("排列顺序")
    @Column()
    private Integer sortNo;

    @ApiModelProperty(value = "URL路径模式")
    @Comment("URL路径模式")
    @Column()
    @NotBlank(message = "URL路径不能为空")
    private String pattern;

    @ApiModelProperty(value = "URL的HTTP方法[COM:HTTP_METH]")
    @Comment("URL的HTTP方法[COM:HTTP_METH]")
    @Column(length = 32)
    String httpMethod;

    @ApiModelProperty(value = "菜单是否隐藏")
    @Comment("菜单是否隐藏")
    @Column()
    @Convert(converter = BooleanToIntegerConverter.class)
    Boolean isHidden;

    @Transient
    Integer menuDepth;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof SysPermissionDO)) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        SysPermissionDO entity = (SysPermissionDO) o;
        return getId().equals(entity.getId());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId());
    }
}